﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;

namespace ServerListerner.Helper
{
    public static class PublicEvent
    {
        private static JsonSerializerOptions option = new JsonSerializerOptions()
        {
            Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
        };
        public static bool IsJson(this string? _str_json)
        {

            try
            {
                if (_str_json == null) return false;
                JsonSerializer.Deserialize(_str_json, typeof(object), option);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
        public static string? GetDescription(this Enum value)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name != null)
            {
                FieldInfo field = type.GetField(name);
                if (field != null)
                {
                    DescriptionAttribute? attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                    if (attr != null)
                    {
                        return attr.Description;
                    }
                }
            }
            return string.Empty;
        }
        public static string ClearSpace(this string _str)
            => _str.Replace("\n", string.Empty)
            .Replace("\0", string.Empty)
            .Replace("\r", string.Empty)
            .Replace("\t", string.Empty)
            .Replace(" ", string.Empty);

        public static string ObjectToJson(this object? _obj)
        {
            try
            {
                if (_obj == null) return string.Empty;
                return JsonSerializer.Serialize(_obj, option);
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
       
        public static object? JsonToObject(this string _str, Type _type) {
            try
            {
                object? obj = Activator.CreateInstance(_type);
                if (string.IsNullOrEmpty(_str))
                    return obj;
                obj = JsonSerializer.Deserialize(_str, _type, option);
                return obj;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static T JsonToObject<T>(this string _str)
        {
            try { 
                if (string.IsNullOrEmpty(_str))
                    return default(T);
                return JsonSerializer.Deserialize<T>(_str, option)??default(T);
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }


        internal static bool IsCustomClass(this Type type)
        {
            if(type==typeof(string)) return false;
            return type.IsClass && !type.IsPrimitive && !type.IsValueType && !type.IsEnum;
         }
    }
}
